
/*
 * Copyright (C) 2008-2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: CollectionField.java 377 2010-03-11 20:50:18Z archie.cobbs $
 */

package org.dellroad.sidekar;

import java.util.Collection;

import org.dellroad.sidekar.query.QueryCollection;

/**
 * A {@link Field} with collection semantics, where the elements of the set are {@link SimpleField}s.
 *
 * @param <E> collection element type
 */
public abstract class CollectionField<E> extends ComplexField<Collection<E>> {

    private final SimpleField<E> elementField;

    CollectionField(String name, Entity<?> entity, Class<?> type, SimpleField<E> elementField) {
        super(name, entity, type);
        this.elementField = elementField;
    }

    /**
     * @throws DetachedItemException {@inheritDoc}
     * @throws IllegalArgumentException {@inheritDoc}
     * @throws IllegalStateException {@inheritDoc}
     * @throws NullPointerException {@inheritDoc}
     * @throws org.dellroad.sidekar.util.SidekarException {@inheritDoc}
     */
    @Override
    public abstract QueryCollection<E> get(Item<?> item);

    /**
     * Get the field type that corresponds to this collection's element type.
     *
     * <p>
     * The returned field will have no name associated with it and cannot be used
     * to set any values.
     */
    public final SimpleField<E> getElementField() {
        return this.elementField;
    }

    @Override
    public final boolean canReference(Entity<?> entity) {
        return this.elementField.canReference(entity);
    }
}

